package cn.edu.zut.sunday.news.controller;

import cn.edu.zut.sunday.base.controller.GenericController;
import cn.edu.zut.sunday.blimp.domain.DatasetName;
import cn.edu.zut.sunday.news.domain.NewsCategory;
import cn.edu.zut.sunday.news.service.NewsCategoryService;
import cn.edu.zut.sunday.news.vo.NewsCategoryQuery;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.persistence.criteria.Predicate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api("mr-新闻分类接口")
@Slf4j
@RestController
@RequestMapping( "newsCategory")
@CrossOrigin
public class NewsCategoryController extends GenericController<NewsCategory,Integer, NewsCategoryService> {
    @Autowired
    public void setNewsCategoryService(NewsCategoryService newsCategoryService) {
        this.service = newsCategoryService;
    }

    @Override
    protected Pageable createPageable(int page, int size) {
        Sort sort  =  Sort.by(Sort.Direction.DESC,"id");
        return PageRequest.of(page, size, sort);
    }

    /**
     * 返回所有category的id和categoryName组成的映射表。
     * 比如
     *  id2NameMap(id -> categoryName)： 1->dataset1, 2->dataset2....；
     * @return 一个Map，有一个Key：id2NameMap
     */
    @ApiOperation(value = "返回ID和categoryName的对应表信息")
    @GetMapping("/idList")
    public Map<String, Map<String, String>> idList() {
        final Map<String, String> id2NameMap = this.service.findAll().stream()
          .collect(Collectors.toMap(v -> String.valueOf(v.getId()), NewsCategory::getCategory));

        return ImmutableMap.<String, Map<String, String>>builder()
          .put("id2NameMap", id2NameMap)
          .build();
    }

    @Override
    protected Specification<NewsCategory> createSpecification(String requestBodyStr) {
        log.info("requestBodyStr: {}", requestBodyStr);
        final NewsCategoryQuery newsCategoryQuery = new Gson().fromJson(requestBodyStr,
                NewsCategoryQuery.class);
        log.info("InformationQuery: {}", newsCategoryQuery);

        if (newsCategoryQuery != null) {
            return getnewsCategorySpecification(newsCategoryQuery);
        }
        return null;
    }

    //分页条件查询
    private Specification<NewsCategory> getnewsCategorySpecification(
            NewsCategoryQuery newsCategoryQuery) {
        String category=newsCategoryQuery.getCategory();

        return (root, query, criteriaBuilder) -> {
            query.distinct(true);
            List<Predicate> list = new ArrayList<>();
            if(!StringUtils.isEmpty(category )){
                Predicate p1 = criteriaBuilder.like(root.get("category"), sqlLike(category));
                list.add(p1);
            }

            return criteriaBuilder.and(list.toArray(new Predicate[0]));
        };
    }


}
